
define(['request','template','jquery'],(request,template) =>{
    
    class  Header {
        constructor (){
            this.load().then( ()=>{
                this.search()
                this.QRcode()
                this.getTabs()
                this.shopNum()
            })
            
        }
        load (){
            return new Promise( resolve =>{
                $('header').load('/html/modules/header.html',resolve)
            })   
        }
        //搜索功能
        search(){
            
            $(".search_btn_input").on('keyup',function(){
                let keyword = $(this).val();//得到当前的value值;
                $.getJSON(
                    `https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=${keyword}&cb=?`,
                    resp =>{
                        $('.search_btn_input_ul').html('');
                        resp.s.forEach(item =>{
                            $(`<li>${item}</li>`).appendTo('.search_btn_input_ul')
                        })
                    }
                );
                $('.search_btn_input_ul').on('mousedown','li',function(e){
                    $('.search_btn_input').val($(e.target).html())
                    $('.search_btn_input_ul').html('')
                });
                $('.search_btn_input').on('blur',()=>{
                    $('search_btn_input_ul').html('')
                })
            })
        }
        //微信显示功能
        QRcode (){
            $('.weixin_icon').on('mouseenter', () =>{
                
                $('.weixin_QRcode').removeClass("show")

            })
            $('.weixin_icon').on('mouseleave', ()=>{
                $('.weixin_QRcode').addClass("show")
            })
            
        }
        //头部的导航栏的渲染
        getTabs(){
            //调用模块的方法，完成数据请求
            request.getTabs().then(resp =>{
                // console.log(resp)
                if (resp.code === 200){
                    let { list } = resp.data
                   
                    list = list.slice(1,6)
                    
                    const  id = window.location.hash.slice(1)
                    
                   $('#tabsList').html(template('tabsTemlpate', { list , id }))

                     

                }

            })

        };
        shopNum(){
            // console.log(123)
                let cartList = localStorage.getItem('cart')
               
               if(cartList){
                    cartList = JSON.parse(cartList)
                    // console.log(cartList)
                    let num = cartList.reduce((total,cart) =>{
                        total  += cart.num
                        return total
                    },0)
                    // console.log(num)
                    $('#shopnumber').html(num)
               }else{
                   $('#shopnumber').html(0)
               }
            }
    
    
    
    
        




    }


    return new Header()

});



